<template>
	<view class="content"> 
	<!-- 推广海报 -->
	<view>
		<view class="myextension"  >
		 
		<block>
			 <image class="imgs" :style="'width:' + windowWidth + 'px;height:540px'"  :src="posterUrl" mode=""></image>
		</block>
		 
		</view> 
		<view class="shareBtn" style="margin: 16rpx 0;">
		 
			<u-row customStyle="margin: 16rpx 0;">
				<u-col span="4" offset="4">
					<u-button type="success" shape="circle"  text="生成海报" @click="shareFc"></u-button>
				</u-col>
			</u-row>
			<u-button type="primary" openType="share" shape="circle" text="转发到微信朋友圈或好友"  ></u-button>
		</view>
	</view>
		<!-- 生成海报 -->
		<!-- 图片展示由自己实现 -->
		<view class="flex_row_c_c modalView" :class="qrShow?'show':''" @tap="hideQr()">
			<view class="flex_column">
				<view class="backgroundColor-white padding1vh border_radius_10px">
					<image :src="poster.finalPath || ''" mode="widthFix" class="posterImage"></image>
				</view>
				<view class="flex_row marginTop2vh">
					<button style="background: #0081ff; color: #FFFFFF;" type="default" size="mini" @tap.prevent.stop="saveImage()">保存图片</button>
				</view>
			</view>
		</view>
		<!-- 画布 -->
		<view class="hideCanvasView">
			<canvas class="hideCanvas" canvas-id="default_PosterCanvasId" :style="{width: (poster.width||10) + 'px', height: (poster.height||10) + 'px'}"></canvas>
		</view>
	</view>
</template>

<script>
	var that
	import _app from '@/components/QS-SharePoster/app.js';
	import {
		getSharePoster
	} from '@/components/QS-SharePoster/QS-SharePoster.js';
	export default {
		onShareTimeline(res) { //分享到朋友圈
				return { 
					path: "/pages/app/home/home"	//分享默认打开是小程序首页
				}
			}, 
		onShareAppMessage(res) {
				return {
					title: '商品过期管家，让您的商品充满价值！',
					path: '/pages/app/home/home?type=103&userId=' + that.userInfo.id + '&userName=' + that.userInfo.real_name
				}
		    },
		data() {
			return {
				windowHeight: '',
				windowWidth: '',
				posterUrl: 'https://servicetall.oss-cn-beijing.aliyuncs.com/saas/common/337de06b759e04cbdfc07f56d865ce2f33fa2de3.jpg',
				userInfo:{},
				poster: {}, 
				picCode:'/static/logo.png',
				qrShow: false,
				canvasId: 'default_PosterCanvasId'
			}
		},
		onLoad() {
			that = this
			let userInfo = uni.getStorageSync('userInfo')
			
			this.getSystem() 
			if(userInfo){
				
				// that.getPoster()
				that.userInfo = userInfo
				// #ifdef MP-WEIXIN
				uni.$u.mpShare.title = '商品过期管家，让您的商品充满价值！';
				uni.$u.mpShare.path = 'pages/app/home/home?type=share&id='+userInfo.id;
				// #endif
				that.getChannelCode()	
			}else{
				 // #ifdef MP-WEIXIN
				 uni.$u.mpShare.title = '商品过期管家，让您的商品充满价值！';
				 //这里贴小程序码
				 uni.$u.mpShare.path = 'pages/app/home/home' 
				 // #endif
						
			} 
		},
		methods: {
			getPoster(){
				//准备获取背景 
				this.mNetTool.getAjaxUrlValueBackground('capi/V2/poster/ERPoster/list', {},
					  (res) =>{
					 uni.setStorageSync('posterUrl',res.rows[0]) 
						that.posterUrl = res.rows[0]
					}) 
				 
				
			},
			getChannelCode() {
				//准备获取二维码
			    let _scene = 'type=103&userId='+ that.userInfo.id 
			    let _page = 'pages/app/home/home'
				let picCode = uni.getStorageSync('picCode')
				if(picCode){
					that.picCode = res.row
				}else{
					this.mNetTool.getAjaxUrlValueBackground('capi/weapp/tools/litPicture', {
					    width: 960,
					    scene: _scene,
					    page: _page,
					  },
					  function (res) {
						uni.setStorageSync('picCode',res.row)
					    console.log('----picCode-----------',res.row)
						that.picCode = res.row
					  })
				}
			   
			  },
			getSystem() {
				let that = this;
				uni.getSystemInfo({
					success: function(res) {  
						that.windowHeight= res.windowHeight,
						that.windowWidth= res.windowWidth 
					 
					}
				});
			},
			async shareFc() {
				try {
					_app.log('准备生成:' + new Date())
					const d = await getSharePoster({
						_this: this, //若在组件中使用 必传
						type: 'testShareType',
						formData: {
							//访问接口获取背景图携带自定义数据

						},
						backgroundImage:'https://servicetall.oss-cn-beijing.aliyuncs.com/saas/common/337de06b759e04cbdfc07f56d865ce2f33fa2de3.jpg',
						posterCanvasId: this.canvasId,	//canvasId
						delayTimeScale: 20, //延时系数
						/* background: {
							width: 1080,
							height: 1920,
							backgroundColor: '#666'
						}, */
						drawArray: ({
							bgObj,
							type,
							bgScale
						}) => {
							const dx = bgObj.width * 0.3;
							const fontSize = bgObj.width * 0.045;
							const lineHeight = bgObj.height * 0.04;
							//可直接return数组，也可以return一个promise对象, 但最终resolve一个数组, 这样就可以方便实现后台可控绘制海报
							return new Promise((rs, rj) => {
								rs([{
										type: 'custom',
										setDraw(Context) {
											Context.setFillStyle('black');
											Context.setGlobalAlpha(0.3);
											Context.fillRect(0, bgObj.height - bgObj.height * 0.2, bgObj.width, bgObj.height * 0.2);
											Context.setGlobalAlpha(1);
										}
									},
									{
										type: 'image',
										url: that.picCode,
										alpha: 1,
										dx,
										dy: bgObj.height - bgObj.width * 0.25,
										infoCallBack(imageInfo) {
											let scale = bgObj.width * 0.2 / imageInfo.height;
											return {
												circleSet: {
													x: imageInfo.width * scale / 2,
													y: bgObj.width *2 / 2,
													r: bgObj.width * 2 / 2
												}, // 圆形图片 , 若circleSet与roundRectSet一同设置 优先circleSet设置
												dWidth: imageInfo.width * scale,
												 // 因为设置了圆形图片 所以要乘以2
												dHeight: bgObj.width * 0.2,
												/* roundRectSet: { // 圆角矩形
													r: imageInfo.width * .1
												} */
											}
										}
									},
									{
										type: 'text',
										fontStyle: 'italic',
										text: '商品过期管家',
										size: fontSize,
										color: 'white',
										alpha: .5,
										textAlign: 'left',
										textBaseline: 'middle',
										infoCallBack(textLength) {
											_app.log('index页面的text的infocallback ，textlength:' + textLength);
											return {
												dx: bgObj.width - textLength - fontSize,
												dy: bgObj.height - lineHeight * 3
											}
										},
										serialNum: 0,
										id: 'tag1'	//自定义标识
									},
									{
										type: 'text',
										text: '商品过期管家',
										fontWeight: 'bold',
										size: fontSize,
										color: 'white',
										alpha: .75,
										textAlign: 'left',
										textBaseline: 'middle',
										serialNum: 1,
										allInfoCallback({	//v3.0.1 更新 可以获取drawArray中全部数据
											drawArray
										} = {}) {
											const obj = drawArray.find(item => item.id === 'tag1');
											/* return {
												dx: obj.dx,
												dy: obj.dy + lineHeight
											} */
											//也可以return promise对象
											return new Promise((rs, rj) => {
												setTimeout(() => {
													rs({
														dx: obj.dx,
														dy: obj.dy + lineHeight
													});
												}, 1);
											});
										}
									},
									{
										type: 'text',
										text: '商品过期管家',
										size: fontSize,
										color: 'white',
										alpha: 1,
										textAlign: 'left',
										textBaseline: 'middle',
										infoCallBack(textLength) {
											return {
												dx: bgObj.width - textLength - fontSize,
												dy: bgObj.height - lineHeight
											}
										}
									},
									// {
									// 	type: 'qrcode',
									// 	text: '欢迎使用商品过期管家',
									// 	size: bgObj.width * 0.2,
									// 	dx: bgObj.width*0.05,
									// 	dy: bgObj.height - bgObj.width*0.25
									// }
								]);
							})
						},
						setCanvasWH: ({
							bgObj,
							type,
							bgScale
						}) => { // 为动态设置画布宽高的方法，
							this.poster = bgObj;
						}
					});
					_app.log('海报生成成功, 时间:' + new Date() + '， 临时路径: ' + d.poster.tempFilePath)
					this.poster.finalPath = d.poster.tempFilePath;
					this.qrShow = true;
				} catch (e) {
					_app.hideLoading();
					_app.showToast(JSON.stringify(e));
					console.log(JSON.stringify(e));
				}
			},
			saveImage() {
				// #ifndef H5
				uni.saveImageToPhotosAlbum({
					filePath: this.poster.finalPath,
					success(res) {
						_app.showToast('保存成功');
					}
				})
				// #endif
				// #ifdef H5
				_app.showToast('保存了');
				// #endif
			},
			hideQr() {
				this.qrShow = false;
			}
		}
	}
</script>

<style>
	.hideCanvasView {
		position: relative;
	}

	.hideCanvas {
		position: fixed;
		top: -99999upx;
		left: -99999upx;
		z-index: -99999;
	}

	.flex_row_c_c {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.modalView {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		opacity: 0;
		outline: 0;
		transform: scale(1.2);
		perspective: 2500upx;
		background: rgba(0, 0, 0, 0.6);
		transition: all .3s ease-in-out;
		pointer-events: none;
		backface-visibility: hidden;
		z-index: 999;
	}

	.modalView.show {
		opacity: 1;
		transform: scale(1);
		pointer-events: auto;
	}

	.flex_column {
		display: flex;
		flex-direction: column;
	}

	.backgroundColor-white {
		background-color: white;
	}

	.border_radius_10px {
		border-radius: 10px;
	}

	.padding1vh {
		padding: 1vh;
	}

	.posterImage {
		width: 60vw;
	}

	.flex_row {
		display: flex;
		flex-direction: row;
	}

	.marginTop2vh {
		margin-top: 2vh;
	}
	.blueBtn{
		width: 500rpx;
		margin: 50rpx auto;
		display: block;
		/* line-height: 80rpx; */
		line-height: 80rpx;
	}
</style>
